<?php

namespace app\admin\controller;

use think\Controller;
use Util\SysDb;
use think\Db;

class User extends Controller
{
    public function alllist()
    {
        // ->where(['store_id' => $storeId])->lists()
        $this->db = new SysDb();

        $count = $this->db->table('tenant_info')->lists();

        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $count]));
    }

    public function userlist()
    {

        $tenantId = trim(input('param.tenantId'));
        $phone = trim(input('param.phone'));
        $parentId=trim(input('param.parentId'));
        $id=trim(input('param.id'));
        // ->where(['store_id' => $storeId])->lists()
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        
        $where=array(
            "phone" => $phone,
            "tenant_id" => $tenantId,
            "parent_id" =>$parentId,
            "id"=>$id
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $tol = (intval($page) - 1) * $limit;
      
            $list = DB::name("bus_info")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
            $this->db = new SysDb();
            $count = $this->db->table('bus_info')->where($where)->count();
       

        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function userdetail()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        $tenant = $this->db->table('tenant_info')->where(['id' => $bus['tenant_id']])->item();
        $data = array();
        $data['info'] = $bus;
        $data['tenantInfo'] = $tenant;
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function addflow()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.busId'));
        $tenant_id = trim(input('param.tenant_id'));
        $amount = trim(input('param.amount'));
        $dealDesc = trim(input('param.dealDesc'));
        $dealType = trim(input('param.dealType'));
        $type = trim(input('param.type'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        if ($type == 1) {
            $bus['balance'] = $bus['balance'] + $amount;
        } else {
            $bus['balance'] = $bus['balance'] - $amount;
            if ($bus['balance'] < 0) {
                exit(json_encode(['code' => 1, 'message' => 'error', "result" => "账户资金不够支出"]));
            }
        }
        $buses = $this->db->table('bus_info')->where(['id' => $id])->update($bus);
        $buss["amount"] = $amount;
        $buss["tenant_id"] = $tenant_id;
        $buss["bus_id"] = $id;
        $buss["deal_desc"] = $dealDesc;
        $buss["remark"] = $dealDesc;
        $buss["type"] = $type;
        $buss["deal_type"] = $dealType;
        // switch ($type) {
        //     case 1:
        //         $buss["type"] = "入账";
        //         break;
        //     default:
        //     $buss["type"] = "支出";
        // }
        // switch ($dealType) {
        //     case 1:
        //         $buss["deal_type"] = "充值";
        //         break;
        //     case 2:
        //         $buss["deal_type"] = "下单支出";
        //     case 3:
        //         $buss["deal_type"] = "推广获利";
        //         break;
        //     case 4:
        //         $buss["deal_type"] = "包裹退款";
        //         break;
        //     case 5:
        //         $buss["deal_type"] = "提现支出";
        //         break;
        //     case 6:
        //         $buss["deal_type"] = "提现失败";
        //         break;
        //     default:
        //     $buss["deal_type"] = "其他";
        // }
        if ($buss["deal_type"] == 1 && $buss["type"] == 1) {
            $summany = DB::name("sys_summary")->order("id desc")->find();
            $idw = $summany['id'];
            $arraym = explode(" ", $summany['create_time']);
            if ($arraym[0] == date("Y-m-d")) {
                $summany['recharge'] = $summany['recharge'] + $amount;
                $this->db->table('sys_summary')->where(['id' => $idw])->update($summany);
            } else {
                $summany['id'] = $summany['id'] + 1;
                $summany['tenant_id'] = trim(input('param.tenant_id'));
                $summany['bus_num'] = 0;
                $summany['recharge'] = $amount;
                $summany['order_num'] = 0;
                $summany['pk_num'] = 0;
                $summany['profit'] = 0;
                $summany['tenant_profit'] = 0;
                $summany['create_time'] = date('Y-m-d h:i:s', time());;
                $this->db->table('sys_summary')->insert($summany);
            }
        }
        $busess = $this->db->table('bus_flow')->insert($buss);
        $bussg["money"] = $amount;
        $bussg["remark"] = $dealDesc;
        $bussg["type"] = $type;
        $bussg["status"] = 1;
        $busesg = $this->db->table('ad_flow')->insert($bussg);
        if ($busess || $buses || $busesg) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
        }
    }
    public function userfreezeOrThaw()
    {
        $id = trim(input('param.id'));
        $status = trim(input('param.status'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        $bus['status'] = $status;
        $buses = $this->db->table('bus_info')->where(['id' => $id])->update($bus);
        if ($buses) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
    }
    public function userupdateWithdrawStatus()
    {
        $id = trim(input('param.id'));
        $withdrawStatus = trim(input('param.withdraw_status'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        $bus['withdraw_status'] = $withdrawStatus;
        $buses = $this->db->table('bus_info')->where(['id' => $id])->update($bus);
        if ($buses) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $bus]));
        }
    }

    public function useraftersalelist()
    {
        // ->where(['store_id' => $storeId])->lists()
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $busId  = trim(input('param.busId'));
        $status  = trim(input('param.status'));
        $tenantId  = trim(input('param.tenantId'));
        $where=array(
            "bus_id" => $busId,
            "tenant_id" => $tenantId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $tol = (intval($page) - 1) * $limit;
       
            $list = DB::name("bus_after_sale")->order("create_time desc")->where($where)->where(['status' => $status])->limit($tol, $limit)->select();
            $this->db = new SysDb();
            $count = $this->db->table('bus_after_sale')->where($where)->where(['status' => $status])->count();
        // else if ($tenantId != "" && $busId == "") {
        //     $list = DB::name("bus_after_sale")->order("create_time desc")->where(['status' => $status, 'tenant_id' => $tenantId])->limit($tol, $limit)->select();
        //     $this->db = new SysDb();
        //     $count = $this->db->table('bus_after_sale')->where(['status' => $status, 'tenant_id' => $tenantId])->count();
        // } else if ($tenantId == "" && $busId != "") {
        //     $list = DB::name("bus_after_sale")->order("create_time desc")->where(['status' => $status, 'bus_id' => $busId])->limit($tol, $limit)->select();
        //     $this->db = new SysDb();
        //     $count = $this->db->table('bus_after_sale')->where(['status' => $status, 'bus_id' => $busId])->count();
        // } else {
        //     $list = DB::name("bus_after_sale")->order("create_time desc")->where(['status' => $status, 'bus_id' => $busId, 'tenant_id' => $tenantId])->limit($tol, $limit)->select();
        //     $this->db = new SysDb();
        //     $count = $this->db->table('bus_after_sale')->where(['status' => $status, 'bus_id' => $busId, 'tenant_id' => $tenantId])->count();
        // }

        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function userupdateStatus()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $remark = trim(input('param.remark'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_after_sale')->where(['id' => $id])->item();
        $bus['remark'] = $remark;
        $bus['status'] = 1;
        $buses = $this->db->table('bus_after_sale')->where(['id' => $id])->update($bus);
        if ($buses) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
        }
    }

    public function userbottomsheetlist()
    {
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $status  = trim(input('param.status'));
        $busId  = trim(input('param.busId'));
        $tenantId  = trim(input('param.tenantId'));
        $where=array(
            "bus_id" => $busId,
            "tenant_id" => $tenantId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("bus_bottom_sheet")->order("create_time desc")->where($where)->where(['status' => $status])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_bottom_sheet')->where($where)->where(['status' => $status])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function userbottomsheetlistupdate()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_bottom_sheet')->where(['id' => $id])->item();
        $bus['status'] = 1;
        $buses = $this->db->table('bus_bottom_sheet')->where(['id' => $id])->update($bus);
        if ($buses) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
        }
    }

    public function userflowList()
    {
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $type  = trim(input('param.type'));
        $busId  = trim(input('param.busId'));
        $tenantId  = trim(input('param.tenantId'));
        $where=array(
            "bus_id" => $busId,
            "tenant_id" => $tenantId,
            "type" => $type
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("bus_flow")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_flow')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function useradviceList()
    {
        $status  = trim(input('param.status'));
        $suggestType  = trim(input('param.suggestType'));
        $busId  = trim(input('param.busId'));
        $tenantId  = trim(input('param.tenantId'));
        $where=array(
            "bus_id" => $busId,
            "tenant_id" => $tenantId,
            "suggest_type" => $suggestType
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
       
            $list = DB::name("sys_advice")->order("create_time desc")->where($where)->where(['status' => $status])->limit($tol, $limit)->select();
            $this->db = new SysDb();
            $count = $this->db->table('sys_advice')->where($where)->where(['status' => $status])->count();
            $data = array();
            array_push($data, $list);
            array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function userely()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $content = trim(input('param.content'));
        $this->db = new SysDb();
        $bus = $this->db->table('sys_advice')->where(['id' => $id])->item();
        $bus['status'] = 1;
        $bus['content'] = $content;
        $buses = $this->db->table('sys_advice')->where(['id' => $id])->update($bus);
        if ($buses) {
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
        }
    }
}
